using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using umbraco.cms.presentation.Trees;
using umbraco.interfaces;
using umbraco.cms.businesslogic.language;
using Commerce.Common;
using System.Xml;


namespace CommerceForUmbraco.adminSection.trees
{
    /// <summary>
    /// Renders a Tree containing all Products
    /// </summary>
    public class loadProductTree : BaseTree
        {
            public loadProductTree(string application) : base(application) { }

            protected override void CreateRootNode(ref XmlTreeNode rootNode)
            {
                rootNode.Icon = FolderIcon;
                rootNode.OpenIcon = FolderIconOpen;
                rootNode.NodeType = "init" + TreeAlias;
                rootNode.NodeID = "init";
            }

            public override void RenderJS(ref StringBuilder Javascript)
            {
                Javascript.Append(
                    @"
                    function openProduct(id) {
	                    parent.right.document.location.href = 'commerce/editProduct.aspx?p=' + id;
                    }
                ");
            }

            public override void Render(ref XmlTree tree)
            {

                string letter = string.Empty;
                if (HttpContext.Current.Request.QueryString.ToString().IndexOf("letter") >= 0)
                {
                    letter = HttpContext.Current.Request.QueryString.Get("letter");
                }

                if (letter != string.Empty)
                {
                    // get collection values
                    Commerce.Common.ProductCollection products = null;


                    if (letter == "Others")
                    {
                        SubSonic.InlineQuery qry = new SubSonic.InlineQuery();
                        products = new ProductCollection();
                        products = qry.ExecuteAsCollection<ProductCollection>("select * from dbo.CSK_Store_Product where substring(productname, 1, 1) in ('1','2','3','4','5','6','7','8','9','0')");
                    }
                    else
                        products = new ProductCollection().Where(Product.Columns.ProductName, SubSonic.Comparison.Like, letter + "%").OrderByAsc("ProductName").Load();

                    foreach (Product thisProd in products)
                    {
                        XmlTreeNode xNode = XmlTreeNode.Create(this);
                        xNode.NodeID = thisProd.ProductID.ToString();
                        xNode.Text = thisProd.ProductName;
                        xNode.Action = "javascript:openProduct(" + thisProd.ProductID + ");";
                        xNode.Icon = "statistik.gif";
                        xNode.OpenIcon = "statistik.gif";

                        tree.Add(xNode);
                    }
                }
                else
                {
                  
                    for (int i = 97; i < 123; i++)
                    {

                        XmlTreeNode xNode = XmlTreeNode.Create(this);
                        xNode.NodeID = i.ToString();
                        xNode.Text = ((char)i).ToString();
                        xNode.Source = "tree.aspx?letter=" + ((char)i) + "&app=" + m_app + "&treeType=" +
                            HttpContext.Current.Request.QueryString["treeType"] + "&rnd=" + Guid.NewGuid();
                        xNode.Icon = FolderIcon;
                        xNode.OpenIcon = FolderIcon;

                        tree.Add(xNode);

         
                    }

                    //Add folder named "Others"
                    XmlTreeNode otherNode = XmlTreeNode.Create(this);
                    otherNode.NodeID = "0";
                    otherNode.Text = "Others";
                    otherNode.Source = "tree.aspx?letter=Others&app=" + m_app + "&treeType=" +
                        HttpContext.Current.Request.QueryString["treeType"] + "&rnd=" + Guid.NewGuid();
                    otherNode.Icon = FolderIcon;
                    otherNode.OpenIcon = FolderIcon;

                    tree.Add(otherNode);

                }
            }
        }


    }
